package com.bosicloud.cmb.ibp.aws.adapter.utils;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.model.InstanceType;
import com.bosicloud.cmb.ibp.commom.constant.IbpError;
import com.bosicloud.cmb.ibp.commom.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Jack Peng / ziliang.peng@bosicloud.com
 */
@Slf4j
public class AWSUtils {


    public static Regions checkRegion(String region) {
        try {
            return Regions.fromName(region.trim().toLowerCase());
        } catch (IllegalArgumentException e) {
            throw new BusinessException(String.format("Region参数错误, 未发现Region : %s", region));
        }
    }

    public static void chechEC2Type(String type) {
        try {
            InstanceType.fromValue(type.trim().toLowerCase());
        } catch (IllegalArgumentException e) {
            throw new BusinessException(String.format("EcsInstanceType参数错误, 未发现此实例类型 : %s", type.trim()));
        }
    }

    public static AWSStaticCredentialsProvider credentialsProvider(String accessKey,String secretKey) {
        return new AWSStaticCredentialsProvider(
                new BasicAWSCredentials(accessKey.trim(), secretKey.trim()));
    }

    public static String awsException2ChineseAndPrint(Exception e) {

        if(e instanceof com.amazonaws.services.apigateway.model.BadRequestException) {
            log.error(IbpError.AWS_BAD_REQUEST_EXCEPTION.code(), e);
            return IbpError.AWS_BAD_REQUEST_EXCEPTION.getTips();
        }

        if(e instanceof com.amazonaws.services.apigateway.model.LimitExceededException
                || e instanceof com.amazonaws.services.cloudformation.model.LimitExceededException) {
            log.error(IbpError.AWS_LIMIT_EXCEEDED_EXCEPTION.code(), e);
            return IbpError.AWS_LIMIT_EXCEEDED_EXCEPTION.getTips();
        }

        if(e instanceof com.amazonaws.services.apigateway.model.UnauthorizedException) {
            log.error(IbpError.AWS_UNAUTHORIZED_EXCEPTION.code(), e);
            return IbpError.AWS_UNAUTHORIZED_EXCEPTION.getTips();
        }

        if(e instanceof com.amazonaws.services.apigateway.model.TooManyRequestsException) {
            log.error(IbpError.AWS_TOO_MANY_REQUESTS_EXCEPTION.code(), e);
            return IbpError.AWS_TOO_MANY_REQUESTS_EXCEPTION.getTips();
        }

        if (e instanceof com.amazonaws.services.apigateway.model.ConflictException) {
            log.error(IbpError.AWS_CONFLICT_EXCEPTION.code(), e);
           return IbpError.AWS_CONFLICT_EXCEPTION.getTips();
        }

        if(e instanceof com.amazonaws.services.cloudformation.model.AlreadyExistsException) {
            log.error(IbpError.AWS_ALREADY_EXISTS_EXCEPTION.code(), e);
            return IbpError.AWS_ALREADY_EXISTS_EXCEPTION.getTips();
        }

        if(e instanceof com.amazonaws.services.cloudformation.model.TokenAlreadyExistsException) {
            log.error(IbpError.AWS_TOKEN_ALREADY_EXISTS_EXCEPTION.code(), e);
            return IbpError.AWS_TOKEN_ALREADY_EXISTS_EXCEPTION.getTips();
        }

        if(e instanceof com.amazonaws.services.cloudformation.model.InsufficientCapabilitiesException) {
            log.error(IbpError.AWS_INSUFFICIENT_CAPABILITIES_EXCEPTION.code(), e);
            return IbpError.AWS_INSUFFICIENT_CAPABILITIES_EXCEPTION.getTips();
        }

        log.error("AWS_UNKNOWN_0", e);
        return e.getMessage();

    }

}
